package com.pri.chat.nim.session.viewholder;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.netease.nim.uikit.R;
import com.netease.nim.uikit.common.ui.recyclerview.adapter.BaseMultiItemFetchLoadAdapter;
import com.netease.nim.uikit.impl.NimUIKitImpl;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.PayResultBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.msg.MsgService;
import com.pri.chat.activity.NetPicShowActivity;
import com.pri.chat.model.RechargePayModel;
import com.pri.chat.model.SImageModel;
import com.pri.chat.model.SMModel;
import com.pri.chat.nim.DemoCache;
import com.pri.chat.nim.session.activity.WatchSnapChatPictureActivity;
import com.pri.chat.nim.session.extension.SImageAttachment;
import com.pri.chat.pay.PayUtils;
import com.pri.chat.utils.Des3Util;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxDataTool;
import com.vondear.rxui.view.dialog.RxDialogSureCancel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/17
 */
public class MsgViewHolderSImage extends MsgViewHolderThumbSImage {


    public MsgViewHolderSImage(BaseMultiItemFetchLoadAdapter adapter) {
        super(adapter);
    }

    @Override
    protected String thumbFromSourceFile(String path) {
        return path;
    }

    @Override
    public int getContentResId() {
        return com.netease.nim.uikit.R.layout.nim_message_item_picture;
    }

    private void initDialog(String content){
        RxDialogSureCancel rxDialogEditSureCancel =new RxDialogSureCancel(context);
        rxDialogEditSureCancel.getLogoView().setVisibility(View.GONE);
        rxDialogEditSureCancel.getTitleView().setTextSize(18);
        rxDialogEditSureCancel.getTitleView().setTextColor(Color.BLACK);
        rxDialogEditSureCancel.setTitle("提示");
        rxDialogEditSureCancel.setContent(content);
        rxDialogEditSureCancel.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxDialogEditSureCancel.dismiss();
            }
        });
        rxDialogEditSureCancel.setSureListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxDialogEditSureCancel.dismiss();
                httpGetPicture();
            }
        });
        rxDialogEditSureCancel.show();
    }

    private void httpGetPicture() {
        SImageAttachment msgAttachment = (SImageAttachment) message.getAttachment();
        Map<String, Object> map = message.getLocalExtension();
        if (map == null) {
            map = new HashMap<>();
            map.put("image_type",0);
        }
        Map<String, Object> finalMap = map;
        SubscriberOnNextListener<BaseBean<PayResultBean>> onNextListener = baseBean -> {
            finalMap.put("image_type", 1);
            message.setLocalExtension(finalMap);
            message.setAttachment(msgAttachment);
            adapter.notifyDataSetChanged();
            NIMClient.getService(MsgService.class).updateIMMessage(message);
        };
        SMModel rechargePayModel = new SMModel();//登录上传参数
        rechargePayModel.setMemberId(SharedHelper.readId(context));
        rechargePayModel.setOtherMemberId(message.getFromAccount());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(rechargePayModel));
        HttpMethods.getInstance().lookSiMiPic(new ProgressSubscriber<>(onNextListener, context, true), requestBody);
    }

    @Override
    public void onItemClick() {
        Map<String, Object> map = message.getLocalExtension();
        if (map == null) {
            map = new HashMap<>();
            map.put("image_type",0);
        }
        if (!message.getFromAccount().equals(SharedHelper.readId(context)) && (int) map.get("image_type") == 0) {
//            initDialog("支付"+10+"金币查看图片");
            SubscriberOnNextListener<BaseBean<String>> onNextListener = baseBean -> {
                if(baseBean.isSuccess()){
                    if("0".equals(baseBean.getData())){
                        httpGetPicture();
                    }else{
                        initDialog("支付"+baseBean.getData()+"金币查看图片");
                    }
                }else{
                    initDialogGold("您的金币不足，请充值金币！");
                }
            };
            SImageModel rechargePayModel = new SImageModel();
            rechargePayModel.setMemberId(SharedHelper.readId(context));
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(rechargePayModel));
            HttpMethods.getInstance().getSiMiPriceByMemberId(new ProgressSubscriber<>(onNextListener, context, true), requestBody);
        } else {
            // WatchSnapChatPictureActivity.start(context, message);
            SImageAttachment msgAttachment = (SImageAttachment) message.getAttachment();
            String path = msgAttachment.getPath();
            ArrayList<String> pathList = new ArrayList<>();
            pathList.add(path);
            Bundle bundle = new Bundle();
            bundle.putInt("item",0);
            bundle.putSerializable("listPath",pathList);
            RxActivityTool.skipActivity(context, NetPicShowActivity.class,bundle);
        }
    }

    private void initDialogGold(String content) {
        RxDialogSureCancel rxDialogEditSureCancel = new RxDialogSureCancel(context);
        rxDialogEditSureCancel.getLogoView().setVisibility(View.GONE);
        rxDialogEditSureCancel.getTitleView().setTextSize(18);
        rxDialogEditSureCancel.getTitleView().setTextColor(Color.BLACK);
        rxDialogEditSureCancel.setTitle("提示");
        rxDialogEditSureCancel.setSure("去充值");
        rxDialogEditSureCancel.getSureView().setTextColor(context.getResources().getColor(R.color.orange));
        rxDialogEditSureCancel.setCancel("取消");
        rxDialogEditSureCancel.setContent(content);
        rxDialogEditSureCancel.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxDialogEditSureCancel.dismiss();
            }
        });
        rxDialogEditSureCancel.setSureListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NimUIKitImpl.getSessionListener().openVipActivity(context,1);
                rxDialogEditSureCancel.dismiss();
            }
        });
        rxDialogEditSureCancel.show();
    }
}
